<?php
// +----------------------------------------------------------------------
// | ProjectName : domall
// +----------------------------------------------------------------------
// | Description :  登录类
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.idowe.com All rights reserved.
// +----------------------------------------------------------------------
// | Authors : Johhny <chenjf@idowe.com>  Date : 2016-02-03
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Admin;

class Login extends Base{

    /**
     * 构造函数
     * Login constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 登录页面
     * @author Johhny <chenjf@idowe.com>
     */
    public function index(){
        if(IS_POST){
            $username = $_POST['username'];
            $password = $_POST['password'];

            $admin_info = Admin::where(array('admin_name'=>$username))->find();
            if(!empty($admin_info) && $admin_info['admin_password'] == md5($password)){
                if ($admin_info['admin_gid'] > 0) {
                    $gamdin_info = Gadmin::where(array('gid'=>$admin_info['admin_gid']))->find();
                    $group_name = $gamdin_info['gname'];
                } else {
                    $group_name = '超级管理员';
                }
                $array = array();
                $array['name']  = $admin_info['admin_name'];
                $array['id']    = $admin_info['admin_id'];
                $array['time']  = $admin_info['admin_login_time'];
                $array['ip']    = $this->getIp();
                $array['gid']   = $admin_info['admin_gid'];
                $array['gname'] = $group_name;
                $array['sp']    = $admin_info['admin_is_super'];
                $array['qlink'] = $admin_info['admin_quick_link'];
                $this->systemSetKey($array, $admin_info['admin_avatar'], true);
                $update_info    = array(
                    'admin_id'=>$admin_info['admin_id'],
                    'admin_login_num'=>($admin_info['admin_login_num']+1),
                    'admin_login_time'=>time()
                );
                Admin::update($update_info);
                $this->redirect('/admin/index');

            }else{
                return $this->error('用户名或密码错误');
            }

        }else{
            return $this->display();
        }
    }

    /**
     * 退出操作
     * @author Doogie <461960962@qq.com>
     */
    public function logout(){
        session('sys_key',null);
        $this->success('安全退出', url('login/index'));
    }

}